package apollo.game.scene;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;

import apollo.game.core.GameApplication;
import apollo.game.core.Input;

import apollo.game.entity.Map;
import apollo.game.entity.Role;
import apollo.game.screen.Displayable;
import apollo.game.utils.ResHelp;

public class MapScene extends Scene {
	public final static int MOVE_PIXEL = 16;
	
	private Map map;
	private Role role;
	private int mapOffsetX;
	private int mapOffsetY;
	
	private int move_pixel;
	private int moved_pixels;
	private int move_total_pixel;
	private int curDir;
	private int keyCode;
	
	private int curMapCol;
	private int curMapRow;
	private int sceneRow;
	private int sceneCol;
	private int colOffset;
	private int rowOffset;
	
	private int leftEdgeCol;
	private int leftEdgeRow;
	private int rightEdgetCol;
	private int rightEdgetRow;
	
	private int roleOffsetX = 0;
	private int roleOffsetY = 0;
	private int roleOffsetCol = 0;
	private int roleOffsetRow = 0;
	
	
	private int[] roleSequence = {0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 0, 0};
	public MapScene() {
		moved_pixels = 0;
		keyCode = 0;
	}
	
	private void init_map(int newMapCol, int newMapRow) {
		this.curMapCol = newMapCol;
		this.curMapRow = newMapRow;
		this.colOffset = this.curMapCol - (this.sceneCol >> 1);
		this.rowOffset = this.curMapRow - (this.sceneRow >> 1);
		this.mapOffsetX = -(this.curMapCol - this.roleOffsetCol - this.colOffset) << 4;
		this.mapOffsetY = -(this.curMapRow - this.roleOffsetRow - this.rowOffset - 1) << 4;
		this.leftEdgeCol = this.colOffset + this.roleOffsetCol + 2;
		this.leftEdgeRow = this.rowOffset + this.roleOffsetRow + 2;
		this.rightEdgetCol = (this.colOffset + this.sceneCol) - this.roleOffsetCol;
		this.rightEdgetRow = (this.rowOffset + this.sceneRow) - this.roleOffsetRow - 2;
				
		this.map = new Map("1", colOffset, rowOffset, sceneCol, sceneRow);	
		this.role = new Role("1");
		
		System.out.println("load map");
	}
	
	@Override
	public void init() {
		this.roleOffsetCol = ResHelp.powers(GameApplication.getWidth()) + 1;
		this.roleOffsetRow = ResHelp.powers(GameApplication.getHeight());
		this.roleOffsetX = this.roleOffsetCol << 4;
		this.roleOffsetY = this.roleOffsetRow << 4;
		this.sceneRow = ResHelp.pixelToCellWidth(GameApplication.getHeight());
		this.sceneCol = ResHelp.pixelToCellWidth(GameApplication.getWidth());
		
		init_map(40, 20);
	}
	
	@Override
	public void render(SpriteBatch batch, int x, int y, int width, int height) {
		
		if (moved_pixels == move_total_pixel) {
			moved_pixels = 0;
			move_total_pixel = 0;
			
			this.curMapCol = (roleOffsetX - mapOffsetX >> 4) + this.colOffset;
			this.curMapRow = (roleOffsetY - mapOffsetY + 16 >> 4) + this.rowOffset;
			
			if (this.curMapCol < this.leftEdgeCol || this.curMapCol > this.rightEdgetCol) {
				init_map(this.curMapCol, this.curMapRow);
			}
			if (this.curMapRow < this.leftEdgeRow || this.curMapRow > this.rightEdgetRow) {
				init_map(this.curMapCol, this.curMapRow);
			}
			//System.out.println("col:" + curMapCol + " row:" + curMapRow);
			//System.out.println("x:" + mapOffsetX + " y:" + mapOffsetY);
		} else {
			moved_pixels += Math.abs(move_pixel);
			if (curDir == 0) {
				mapOffsetY -= move_pixel;
			} else {
				mapOffsetX -= move_pixel;
			}
		}
		role.setRoleIndex(roleSequence[moved_pixels] + keyCode);
		
		// start render
		batch.begin();
		map.render(batch, mapOffsetX, mapOffsetY, width, height);
		role.render(batch, roleOffsetX, roleOffsetY, width, height);
		map.render(batch, mapOffsetX, mapOffsetY, width, height);
		batch.end();
		// end render
	}

	public void tick(Input input) {
		if (input.buttons[Input.UP]) {
			if (move_total_pixel == 0) {
				move_total_pixel += MOVE_PIXEL;
				move_pixel = -1;
				curDir = 0;
				keyCode = Input.UP;
			}
		} else if (input.buttons[Input.DOWN]) {
			if (move_total_pixel == 0) {
				move_total_pixel += MOVE_PIXEL;
				move_pixel = 1;
				curDir = 0;
				keyCode = Input.DOWN;
			}
		} else if (input.buttons[Input.LEFT]) {
			if (move_total_pixel == 0) {
				move_total_pixel += MOVE_PIXEL;
				move_pixel = -1;
				curDir = 1;
				keyCode = Input.LEFT;
			}
		} else if (input.buttons[Input.RIGHT]) {
			if (move_total_pixel == 0) {
				move_total_pixel += MOVE_PIXEL;
				move_pixel = 1;
				curDir = 1;
				keyCode = Input.RIGHT;
			}
		}
		
	}
	
	private boolean checkMove() {
		boolean enable = false;
	
		return enable;
	}
}
